package com.bocloud.cmp.driver.providers.impl;

import org.springframework.stereotype.Service;

import com.bocloud.cmp.driver.config.Configuration;
import com.bocloud.cmp.driver.providers.RouterDriver;
import com.bocloud.cmp.driver.providers.intf.RouterProvider;
import com.bocloud.cmp.driver.providers.jd.JdRouterDriver;
import com.bocloud.cmp.driver.providers.os.OSDriver.Version;
import com.bocloud.cmp.driver.providers.os.OSRouterDriver;
import com.bocloud.cmp.driver.utils.VendorConverter;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.cmp.model.Butler;
import com.bocloud.cmp.model.RouteModel;
import com.bocloud.common.model.BsmResult;

@Service("routerProvider")
public class RouterProviderImpl implements RouterProvider {

	@Override
	public BsmResult create(RouteModel routeModel, String vendor, String region) {
		BsmResult result = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			RouterDriver driver = new JdRouterDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(),
					region);
			result = driver.create(routeModel);
			break;
		case ALIYUN:
			break;
		case OPENSTACK:
			driver = new OSRouterDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.create(routeModel);
			break;
		case VMWARE:
			break;
		default:
			break;
		}
		return result;
	}

	@Override
	public BsmResult modify(RouteModel routeModel, String vendor, String region) {
		BsmResult result = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			RouterDriver driver = new JdRouterDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(),
					region);
			result = driver.detail(routeModel.getRouteId());
			break;
		case ALIYUN:
			break;
		case OPENSTACK:
			driver = new OSRouterDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.modify(routeModel);
			break;
		case VMWARE:
			break;
		default:
			break;
		}
		return result;
	}

	@Override
	public BsmResult remove(String routerId, String vendor, String region) {
		BsmResult result = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			RouterDriver driver = new JdRouterDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(),
					region);
			result = driver.remove(routerId);
			break;
		case ALIYUN:
			break;
		case OPENSTACK:
			driver = new OSRouterDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.remove(routerId);
			break;
		case VMWARE:
			break;
		default:
			break;
		}
		return result;
	}

	@Override
	public BsmResult list(String vendor, String region) {
		BsmResult result = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			RouterDriver driver = new JdRouterDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(),
					region);
			result = driver.sync();
			break;
		case ALIYUN:
			break;
		case OPENSTACK:
			break;
		case VMWARE:
			break;
		default:
			break;
		}
		return result;
	}

	@Override
	public BsmResult detail(String routerId, String vendor, String region) {
		BsmResult result = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			RouterDriver driver = new JdRouterDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(),
					region);
			result = driver.detail(routerId);
			break;
		case ALIYUN:
			break;
		case OPENSTACK:
			break;
		case VMWARE:
			break;
		default:
			break;
		}
		return result;
	}
}